package org.fhm.zdte.common.task;

import org.fhm.substrate.annotation.Setup;
import org.fhm.zdte.common.constant.protocol.PacketType;
import org.fhm.zdte.common.pojo.Address;
import org.fhm.zdte.common.protocol.AbstractMessageMonitor;
import org.fhm.zdte.common.protocol.AbstractMessageStandard;
import org.fhm.zdte.common.service.transmission.AbstractCmd;

import java.util.Map;


/**
 * The abstract layer for task that deal packet.
 *
 * @author 谭波
 * @since 2023/11/4
 */
public abstract class AbstractPacketDeal extends AbstractTask {

    public AbstractMessageStandard message;

    @Setup
    protected Map<String, AbstractCmd> cmdExecutorContainer;

    @Setup
    protected AbstractMessageMonitor abstractMessageMonitor;

    protected AbstractPacketDeal owner;

    /**
     * Obtain a suitable instruction executor by Packet-Type.
     *
     * @param message Packet-Message.
     * @return Instruction executor
     */
    public AbstractCmd getCmdExecutor(AbstractMessageStandard message) {
        return owner.cmdExecutorContainer.get(
                PacketType
                        .getPacketNameBy(message.getPacketType())
        );
    }

    /**
     * Obtain a task type and submit.
     *
     * @param message Packet-Message.
     * @param address The source address of the Packet-Message.
     */
    public abstract void getTaskAndSubmit(AbstractMessageStandard message, Address address);

}
